package com.hqyj.smvc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hqyj.smvc.mapper.CompanyMapper;
import com.hqyj.smvc.mapper.EmployeeMapper;
import com.hqyj.smvc.mapper.IdcardMapper;
import com.hqyj.smvc.pojo.Company;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.pojo.Idcard;
import com.hqyj.smvc.service.CompanyService;
import com.hqyj.smvc.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("companyService")
@Slf4j
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    @Qualifier("companyMapper")
    private CompanyMapper companyMapper;
    @Autowired
    @Qualifier("employeeMapper")
    private EmployeeMapper employeeMapper;
    @Autowired
    @Qualifier("idcardMapper")
    private IdcardMapper idcardMapper;





    //查看公司信息
    @Override
    public List<Company> exhibitionCompanyList(int i) {
        log.info("CompanyServiceImpl>>>exhibitionCompanyList,start……");
        Page<Company> companyPage = PageHelper.startPage(i,3);
        List<Company> companies = companyMapper.selectCompanyList();
        log.info("CompanyServiceImpl>>>exhibitionCompanyList,end……");
        return companies;
    }

    //修改公司信息
    @Override
    public void companyChange(Company company){
        log.info("CompanyServiceImpl>>>companyChange,start……");
        int i = companyMapper.updateCompany(company);
        log.info("CompanyServiceImpl>>>companyChange,end……");
    }

    //根据company_id查找公司
    @Override
    public Company idCompany(int id){
        log.info("CompanyServiceImpl>>>idCompany,start……");
        Company company = companyMapper.selectIdCompany(id);
        log.info("CompanyServiceImpl>>>idCompany,end……");
        return company;
    }

    //删除公司同时删除此公司的雇员
    @Override
    public void theCollapseOf(int id){
        log.info("CompanyServiceImpl>>>theCollapseOf,start……");
        //删除公司是会删除雇员，删除雇员时，会删除身份证，
        //根据公司id查找雇员的身份证id
        List<Employee> employees = employeeMapper.selectCompanyEmployeeIdcard(id);
        //先删除雇员
        int employee = employeeMapper.deleteCompanyEmployee(id);
        
        for (Employee emp:employees) {
            System.out.println(emp.getIdcard().getIdcard_id());
            //删除身份证
            int i = idcardMapper.deleteIdcard(emp.getIdcard().getIdcard_id());
        }
        //在删除公司，受外键约束
        int company = companyMapper.deleteCompany(id);
        log.info("CompanyServiceImpl>>>theCollapseOf,end……");

    }

    //添加公司
    @Override
    public void createACompany(Company company, String str){
        int company1 = companyMapper.insertCompany(company);
        Integer company_id = company.getCompany_id();
        if (str != "") {
            String[] split = str.split(",");
            for (String st: split) {
                int i = Integer.parseInt(st);
                Employee employee = new Employee();
                employee.setCompany_id(company_id);
                employee.setEmployee_id(i);
                employeeMapper.updateEmployee(employee);
            }
        }
    }

    //查看所有公司
    @Override
    public List<Company> checkExistingCompanies(){
        List<Company> companies = companyMapper.selectCompanyList();
        return companies;
    }
}
